library(ggplot2)
# 4.1.1
plot(cmsy$ref_ts$year, cmsy$ref_ts$b, xlab="Year", ylab="B", type="b", ylim = c(0,max(cmsy$ref_ts$b_hi)))
lines(cmsy$ref_ts$year, cmsy$ref_ts$b_lo, col="red", lty=2)
lines(cmsy$ref_ts$year, cmsy$ref_ts$b_hi, col="red", lty=2)

ggplot(data = cmsy$ref_ts, aes(x = year)) + 
  ylim(c(0, max(cmsy$ref_ts$b_hi))) + 
  geom_line(aes(y = b)) + 
  geom_point(aes(y = b)) + 
  geom_line(aes(y = b_lo), linetype = "dashed", alpha = 0.15) + 
  geom_line(aes(y = b_hi), linetype = "dashed", alpha = 0.15) + 
  geom_ribbon(data = cmsy$ref_ts, aes(ymin = b_lo, ymax = b_hi), alpha = 0.15) + 
  xlab("Year") + 
  ylab("Biomass (B)") + 
  theme_minimal()

# 4.1.2
plot(cmsy$ref_ts$year, cmsy$ref_ts$f, xlab="Year", ylab="F", type="b", ylim = c(0,max(cmsy$ref_ts$f_hi)))
lines(cmsy$ref_ts$year, cmsy$ref_ts$f_lo, col="red", lty=2)
lines(cmsy$ref_ts$year, cmsy$ref_ts$f_hi, col="red", lty=2)

ggplot(data = cmsy$ref_ts, aes(x = year)) + 
  ylim(c(0, max(cmsy$ref_ts$f_hi))) + 
  geom_line(aes(y = f)) + 
  geom_point(aes(y = f)) + 
  geom_line(aes(y = f_lo), linetype = "dashed", alpha = 0.15) + 
  geom_line(aes(y = f_hi), linetype = "dashed", alpha = 0.15) + 
  geom_ribbon(data = cmsy$ref_ts, aes(ymin = f_lo, ymax = f_hi), alpha = 0.15) + 
  xlab("Year") + 
  ylab("Fishing mortality (F)") + 
  theme_minimal()


# 4.3.1 
plot(cmsy$ref_ts$year, cmsy$ref_ts$bbmsy, xlab="Year", ylab="B/Bmsy", type="b", ylim = c(0, 1.5))
lines(cmsy$ref_ts$year, cmsy$ref_ts$bbmsy_lo, col="red", lty=2)
lines(cmsy$ref_ts$year, cmsy$ref_ts$bbmsy_hi, col="red", lty=2)
abline(h=1, col="blue", lty=3)

ggplot(data = cmsy$ref_ts, aes(x = year)) + 
  ylim(c(0, max(cmsy$ref_ts$bbmsy_hi))) + 
  geom_line(aes(y = bbmsy)) + 
  geom_point(aes(y = bbmsy)) + 
  geom_line(aes(y = bbmsy_lo), linetype = "dashed", alpha = 0.15) + 
  geom_line(aes(y = bbmsy_hi), linetype = "dashed", alpha = 0.15) + 
  geom_ribbon(data = cmsy$ref_ts, aes(ymin = bbmsy_lo, ymax = bbmsy_hi), alpha = 0.15) + 
  geom_hline(yintercept = 1, linetype = "dashed", size = 0.3) + 
  geom_text(x = max(cmsy$ref_ts$year)-2, y = 1.05, label = "B/Bmsy = 1", color = "darkgray") +
  xlab("Year") + 
  ylab("B/Bmsy reference") + 
  theme_minimal()

# 4.3.2
plot(cmsy$ref_ts$year, cmsy$ref_ts$ffmsy, xlab="Year", ylab="F/Fmsy", type="b", ylim = c(0, 3))
lines(cmsy$ref_ts$year, cmsy$ref_ts$ffmsy_lo, col="red", lty=2)
lines(cmsy$ref_ts$year, cmsy$ref_ts$ffmsy_hi, col="red", lty=2)
abline(h=1, col="blue", lty=3)

ggplot(data = cmsy$ref_ts, aes(x = year)) + 
  ylim(c(0, max(cmsy$ref_ts$ffmsy_hi))) + 
  geom_line(aes(y = ffmsy)) + 
  geom_point(aes(y = ffmsy)) + 
  geom_line(aes(y = ffmsy_lo), linetype = "dashed", alpha = 0.15) + 
  geom_line(aes(y = ffmsy_hi), linetype = "dashed", alpha = 0.15) + 
  geom_ribbon(data = cmsy$ref_ts, aes(ymin = ffmsy_lo, ymax = ffmsy_hi), alpha = 0.15) + 
  geom_hline(yintercept = 1, linetype = "dashed", size = 0.3) + 
  geom_text(x = max(cmsy$ref_ts$year)-2, y = 1.05, label = "F/Fmsy = 1", color = "darkgray") +
  xlab("Year") + 
  ylab("F/Fmsy reference") + 
  theme_minimal()

# 4.3.3 
plot(cmsy$ref_ts$year, cmsy$ref_ts$b, xlab="Year", ylab="Biomass/Catch", type="b", col = "blue", ylim = c(0, b.max))
abline(h = cmsy$ref_pts$est[3], lty = "dashed", col = "red")
lines(cmsy$ref_ts$year, cmsy$ref_ts$catch, col="red", lty=2, type = "b")
abline(h = cmsy$ref_pts$est[5], lty = "dashed", col = "blue")
legend("topleft", legend = c("B & Bmsy", "C & MSY"), col = c("blue", "red"), lty=c(1,1))

ggplot(data = cmsy$ref_ts, aes(x = year)) + 
  ylim(0, max(cmsy$ref_ts$b_hi)) + 
  xlim(min(cmsy$ref_ts$year), max(cmsy$ref_ts$year)) + 
  geom_line(aes(y = b, color = "B & Bmsy")) + 
  geom_point(aes(y = b, color = "B & Bmsy")) + 
  geom_ribbon(data = cmsy$ref_ts, aes(ymin = b_lo, ymax = b_hi), fill = "blue", alpha = 0.05) + 
  geom_hline(yintercept = cmsy$ref_pts$est[5], linetype = "dotted", size = 0.3, color = "blue") + 
  geom_text(x = max(cmsy$ref_ts$year)-2, y = cmsy$ref_pts$est[5]*1.03, label = "Btr", color = "blue") +
  geom_hline(yintercept = cmsy$ref_pts$est[5]*config.blim_frommsy, linetype = "dashed", size = 0.3, color = "blue") + 
  geom_text(x = max(cmsy$ref_ts$year)-2, y = cmsy$ref_pts$est[5]*config.blim_frommsy*1.03, label = "Blim", color = "blue") +
  geom_line(aes(y = catch, color = "C & CMSY")) + 
  geom_point(aes(y = catch, color = "C & CMSY")) + 
  geom_hline(yintercept = cmsy$ref_pts$est[3], linetype = "dashed", size = 0.3, color = "red") + 
  geom_text(x = max(cmsy$ref_ts$year)-2, y = cmsy$ref_pts$est[3]*1.03, label = "MSY", color = "red") +
  labs(x = "Year", y = "Biomass / Catch", color = "") + 
  scale_color_manual(values = c("Blue", "Red")) +
  theme_minimal() + 
  theme(legend.position = c(0.09, 0.92))


# fig. 6.1.1
plot(cmsy$ref_ts$year, cmsy$ref_ts$b, type="l", ylim = c(0, b.max), lwd=3, xlab="Year", ylab = "B, biomass")
for (i in 1:config.retro.years) {
  lines(retro.data[[i]]$year, retro.data[[i]]$b, col=(i+1))
}

p <- ggplot(data = cmsy$ref_ts, aes(x = year)) +
  ylim(0, b.max) +
  geom_line(aes(y = b, color = "Bdefault"), size = 1.2)

for (i in 1:config.retro.years) {
  d <- as.data.frame(retro.data[[i]])
  col <- paste0("Bt-", max(d$year))
  p = p + geom_line(data = d, aes_string(y = d$b, color = shQuote(col)))
}

p = p + 
  theme_minimal() + 
  labs(x = "Year", y = "Biomass (B)", color = "Retrospective") + 
  theme(legend.position = c(0.04, 0.85))
plot(p)


